package ru.aslanov.schedule.client;

import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import ru.aslanov.schedule.client.ds.InputLangsDS;
import ru.aslanov.schedule.client.ds.SchedulesDS;
import ru.aslanov.schedule.client.i18n.I18nUtil;

import static ru.aslanov.schedule.client.i18n.I18nUtil.cnt;

/**
 * @author Sergey Aslanov
 *         Created: Oct 14, 2010 3:32:17 PM
 */
public class InputLangsPanel extends DataPanel {

    public InputLangsPanel(final ScheduleUser scheduleUser) {
        super(InputLangsDS.getInstance(scheduleUser), cnt().inputLanguage(), "100%", "100%", scheduleUser.getScheduleKey(), scheduleUser.isScheduleAdmin());

        final com.smartgwt.client.widgets.Button makeDefaultButton = new com.smartgwt.client.widgets.Button(I18nUtil.cnt().makeDefaultInputLang());
        makeDefaultButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                final ListGridRecord selectedRecord = getListGrid().getSelectedRecord();
                if (selectedRecord != null) {
                    Record record = new Record();
                    record.setAttribute("encodedKey", scheduleUser.getScheduleKey());
                    record.setAttribute("defaultInputLanguage", selectedRecord.getAttribute("id"));
                    makeDefaultButton.disable();
                    SchedulesDS.getInstance().updateData(record, new DSCallback() {
                        @Override
                        public void execute(DSResponse response, Object rawData, DSRequest request) {
                            makeDefaultButton.enable();
                            getListGrid().invalidateCache();
                        }
                    });
                    /*
                    ScheduleServices.App.getInstance().setDefaultInputLang(selectedRecord.getAttribute("id"),
                            scheduleKey, new MyAsyncCallback<Void>(makeDefaultButton) {
                                @Override
                                public void onSuccess(Void result) {
                                    if (button != null) button.enable();
                                    getListGrid().invalidateCache();
                                }
                            });
                    */
                }
            }
        });
        buttons.addMember(makeDefaultButton);
    }
}
